<?php
namespace app\api\controller\operatingaccount;

use app\api\controller\Base;
use app\common\model\company\Company;
use app\common\model\orderhawa\Hawa;
use app\common\model\orderhawa\HawaList;
use app\common\model\orderreceipt\ReceiptList;
use app\common\model\orderdelivery\DeliveryList;
use app\common\model\orderdelivery\Delivery;
use app\common\model\orderdelivery\Disposalsite;
use app\common\model\operatingaccount\Accountin as AIWC;//入库台账
use app\common\model\operatingaccount\Accountout as AOWC;//入库台账
//经营台账管理器
//机器使用创建系统使用
class Robat extends Base
{
    /**
     * 初始化
     */
    // public function initialize()
    // {
    //     parent::initialize();
    //     self::is_login();
    //     Rolepower::powerguard(101002);
    // }

    /**
     * 产废订单创建完后直接执行的
     * @param  SyncCenter $center [description]
     * @return [type]             [description]
     */
    public static function account_in_warehouse_create($id)
    {
        $data = HawaList::get($id);
        if (empty($data)) {
            return myerr('缺少数据');
        }
        $sbdata = AIWC::where('OrderListId', $id)->find();
        if (!empty($sbdata)) {
            return myerr('此订单已生成入库台账');
        }
        //判断订单状态是否满足创建
        if ((int) $data['State'] == HawaList::STATEOK) {
            $data['company'] = Company::get($data['CompanyId']);
            $data['hawa'] = Hawa::where('HawaListId',$id)->with('areainfo')->select();
            $data['receipt'] = ReceiptList::get($data['ReceiptListId']);
            return json_encode(AIWC::user_create($data));
        } else {
            return myerr('订单还未完结，无法创建台账');
        }
        return myerr('操作失败');
    }
    /**
     * 订单出库后创建完后直接执行的
     * @param  SyncCenter $center [description]
     * @return [type]             [description]
     */
    public static function account_out_warehouse_create($id)
    {
        $data = DeliveryList::get($id);
        if (empty($data)) {
            return myerr('没有找到出库数据');
        }
        $sbdata = AOWC::where('OrderListId', $id)->find();
        if (!empty($sbdata)) {
            return myerr('此订单已生成出库台账');
        }
        //判断订单状态是否满足创建
        //出库订单 出库单位信息
        if ((int) $data['State'] == DeliveryList::STATEOK) {
            $data['delivery'] = Delivery::where('DeliveryListId',$id)->with('stoarea')->select();
            $data['disposalsite'] = Disposalsite::get($data['DisposalSiteId']);
            return json_encode(AOWC::user_create($data));
        } else {
            return myerr('订单还未完结，无法创建台账');
        }
        return myerr('操作失败');
    }

}
